package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.dao.SetmealDao;
import com.itheima.pojo.Member;
import org.apache.ibatis.mapping.ResultMap;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class MemberServiceimpl implements MemberService {
    @Autowired
    SetmealDao setmealDao;
    @Autowired
    MemberDao memberDao;
    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    @Override
    public void addMember(Member member) {
        memberDao.addMember(member);
    }

    @Override
    public Map<String, Object> getMemberReport() {
        List<String>months=new ArrayList<>();
        List<Long> memberCount=new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH,-12);
        for (int i = 0; i <12 ; i++) {
            calendar.add(Calendar.MONTH,1);
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM");
            String date = simpleDateFormat.format(calendar.getTime());
            months.add(date);
        }
        for (String month : months) {
            month+="-31";
            Long count=memberDao.findCountBeforeDate(month);
            memberCount.add(count);
        }
         Map<String,Object>map=new HashMap<>();
        map.put("months",months);
        map.put("memberCount",memberCount);
        return map;
    }

    @Override
    public Map<String, Object> getSetmealReport() {
        List<Map<String,Object>> setmealCount =setmealDao.getSetmealReport();
        List<Object> setmealNames=new ArrayList<>();
        for (Map<String, Object> map : setmealCount) {
            System.out.println("map"+map);
            Object name = map.get("name");
            System.out.println(name);
            setmealNames.add(name);
        }
       Map<String,Object> resultMap=new HashMap<>();
        resultMap.put("setmealNames",setmealNames);
        resultMap.put("setmealCount",setmealCount);

        return resultMap;
    }
}
